package com.lzh.farmersshop.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzh.farmersshop.entity.Userinfo;
import com.lzh.farmersshop.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.lzh.farmersshop.entity.Useradress;
import com.lzh.farmersshop.service.UseradressService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * (Useradress)表控制层
 *
 * @author makejava
 * @since 2024-01-04 15:41:51
 */
@Slf4j
@RestController
@RequestMapping("admin/useradress")
@Transactional(rollbackFor = Exception.class)
public class UseradressController {
    /**
     * 服务对象
     */
    @Resource
    private UseradressService useradressService;

    /**
     * 分页查询所有数据
     *
     * @param useradress 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Useradress useradress,Integer page,Integer limit) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        if (useradress.getUserid() == null || useradress.getUserid() == 0){
            return Result.failure("用户信息获取失败，不能为空");
        }
        LambdaQueryWrapper<Useradress> q = new LambdaQueryWrapper<>();
        q.like(StringUtils.hasLength(useradress.getShname()), Useradress::getShname, useradress.getShname());
        q.like(StringUtils.hasLength(useradress.getShphone()), Useradress::getShphone, useradress.getShphone());
        q.like(StringUtils.hasLength(useradress.getShprovince()), Useradress::getShprovince, useradress.getShprovince());
        q.like(StringUtils.hasLength(useradress.getShjaddress()), Useradress::getShjaddress, useradress.getShjaddress());
        q.eq(Useradress::getUserid, useradress.getUserid());
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", useradressService.list(q).size());
        //分页
        q.last("limit " + (page - 1) * limit + "," + limit);
        result.put("data", useradressService.list(q));
        return Result.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return Result.success(useradressService.getById(id));
    }
}

